package com.kanan.vueupload.service;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 响应接口
 *
 * @author kanan
 */
public interface IResponseWithJson {
    /**
     * 响应json
     *
     * @param request        请求
     * @param response       响应
     * @param responseObject 响应内容
     */
    default void responseWithJson(HttpServletRequest request, HttpServletResponse response, Object responseObject) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try (PrintWriter out = response.getWriter()) {
            out.append(JSONObject.toJSONString(responseObject));
        } catch (IOException var18) {
            var18.printStackTrace();
        }
    }
    /**
     * 错误响应
     * @param status 状态
     * @param response response
     * @param responseObject 响应内容
     */
    default void responseWithJson(HttpServletResponse response,int status, Object responseObject) {
        response.setCharacterEncoding("UTF-8");
        response.setStatus(status);
        response.setContentType("application/json; charset=utf-8");
        try (PrintWriter out = response.getWriter()) {
            out.append(JSONObject.toJSONString(responseObject));
        } catch (IOException var18) {
            var18.printStackTrace();
        }
    }
}